package com.baiyang.app.ui.login;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.model.User;
import com.baiyang.app.tool.DataTool;
import com.baiyang.app.tool.LogUtils;
import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class LoginService {

    private LoginViewModel viewModel;
    private Context context;
    public LoginService(Context context,LoginViewModel viewModel){
        this.context = context;
        this.viewModel = viewModel;
    }

    /**
     * 登录
     * @param user
     */
    public void login(User user){
        JSONObject object = new JSONObject();
        object.put("username",user.getUserName());
        object.put("password", DataTool.getMD5String(user.getPassword()));
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("login",str);
                    viewModel.setLoginResult(JSONObject.parseObject(str));
                }else{
                    LogUtils.i("login","code:" + response.code());
                }
            }

            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("register","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.User.Login,object);
    }
}
